<?php


namespace App\Repositories;


use App\Models\CustomerDocking as Model;
use LTools\Repositories\RepositoryAbstract;

class CustomerDocking extends RepositoryAbstract
{
    /**
     * CustomerDocking constructor.
     * @param Model $model
     * @author luffyzhao@vip.126.com
     */
    public function __construct(Model $model)
    {
        $this->model = $model;
    }

    /**
     * @param array $attributes
     * @param int|null $perPage
     * @param array $columns
     * @param string $pageName
     * @param int|null $page
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     * @author luffyzhao@vip.126.com
     */
    public function paginate(array $attributes, int $perPage = null, array $columns = ['*'], $pageName = 'page', int $page = null)
    {
        return $this->model->with(['customer'])->where(
            $attributes
        )->paginate($perPage, $columns, $pageName, $page);
    }

    /**
     * @param $id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
     * @author luffyzhao@vip.126.com
     */
    public function edit($id){
        /** @var \App\Models\CustomerDocking $model */
        $model = $this->model->findOrFail($id);
        $model->authorities = $model->authorities()->get()->pluck('id');
        return $model;
    }

    /**
     * @param $id
     * @param array $values
     * @return bool|\Illuminate\Database\Eloquent\Model
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function update($id, array $values)
    {
        /** @var \App\Models\CustomerDocking $model */
        $values['secret'] = 'big surprise';
        $model = $this->find($id);
        $model->fill($values)->saveOrFail();
        $model->authorities()->sync($values['authorities']);
    }
}